﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuatToanDAO;
using System.Data.SqlClient;
using System.Data;

public partial class Admin_SourceCodes : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Session["UserName"] == null))
        {
            Response.Redirect("Default.aspx?path=" + Request.Url.ToString());
        }
        
        if (!IsPostBack)
        {
            Panel_Add.Visible = false;
            Panel_Edit.Visible = false;

            CategoryDAO cate = new CategoryDAO();
            cbbCategoryList.DataSource = cate.ExecuteSelectALL().Tables[0]; ;
            cbbCategoryList.DataBind();

            ItemDAO itemcategory = new ItemDAO();
            cbbItemList.DataSource = itemcategory.ExecuteSelectFollowCategoryID(cbbCategoryList.SelectedValue.Trim()).Tables[0];
            cbbItemList.DataBind();

            int itemid = 0;
            int.TryParse(cbbItemList.SelectedValue,out itemid);

            SourceCodeDAO sourcecode = new SourceCodeDAO();
            GridView1.DataSource = sourcecode.ExecuteSelectFollowItemID(itemid).Tables[0];
            GridView1.DataBind();

            //For Add or Edit
            ItemDAO item = new ItemDAO();
            DataTable dt = item.ExecuteSelectALL().Tables[0];

            ItemList_Add.DataSource = dt;
            ItemList_Add.DataBind();

            ItemList_Edit.DataSource = dt;
            ItemList_Edit.DataBind();
        }
        
        
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {

        int sourcecodeid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);

        SourceCodeDAO sourcecode = new SourceCodeDAO();
        List<SqlParameter> parameters = new List<SqlParameter>();
        parameters.Add(new SqlParameter("SourceCodeID", sourcecodeid));
        parameters.Add(new SqlParameter("ItemID", ItemList_Edit.SelectedValue));
        parameters.Add(new SqlParameter("Language", cbbLanguage_Edit.SelectedValue));
        parameters.Add(new SqlParameter("SourceCode", txtCode_Edit.Text));
        parameters.Add(new SqlParameter("SourcePath", txtSourcePath_Edit.Text));

        sourcecode.ExecuteUpdate(parameters);

        cbbItemList_SelectedIndexChanged(null, null);

        lbl_Message.Text = "Edit successfully!";
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int sourcecodeid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
        SourceCodeDAO sourcecode = new SourceCodeDAO();
        DataTable dt = sourcecode.ExecuteSelectFollowSourceCodeID(sourcecodeid).Tables[0];

        ItemList_Edit.SelectedValue = dt.Rows[0]["ItemID"].ToString();
        cbbLanguage_Edit.SelectedValue = dt.Rows[0]["Language"].ToString().Trim() ;
        txtCode_Edit.Text = dt.Rows[0]["SourceCode"].ToString();
        txtSourcePath_Edit.Text = dt.Rows[0]["SourcePath"].ToString();

        Panel_Add.Visible = false;
        Panel_Edit.Visible = true;
    }
    protected void btn_AddItems_Click(object sender, EventArgs e)
    {
        Panel_Add.Visible = true;
        Panel_Edit.Visible = false;

        ItemList_Add.SelectedValue = cbbItemList.SelectedValue;
        cbbLanguage_Add.SelectedValue = "cpp";
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        SourceCodeDAO sourcecode = new SourceCodeDAO();
        List<SqlParameter> parameters = new List<SqlParameter>();

        parameters.Add(new SqlParameter("ItemID", Convert.ToInt32(ItemList_Add.SelectedValue)));
        parameters.Add(new SqlParameter("Language", cbbLanguage_Add.SelectedValue.Trim()));
        parameters.Add(new SqlParameter("SourceCode", txtCode_Add.Text));
        parameters.Add(new SqlParameter("SourcePath", txtSourcePath_Add.Text));
        
        sourcecode.ExecuteInsert(parameters);

        cbbItemList_SelectedIndexChanged(null, null);
        reset();
        lbl_Message.Text = "Add successfully!";

    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        int sourcecodeid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);

        SourceCodeDAO sourcecode = new SourceCodeDAO();
        List<SqlParameter> parameters = new List<SqlParameter>();
        parameters.Add(new SqlParameter("SourceCodeID", sourcecodeid));
        sourcecode.ExecuteDelete(parameters);

        lbl_Message.Text = "Delete successfully!";

        cbbItemList_SelectedIndexChanged(null, null);
        Panel_Edit.Visible = false;
    }

    protected void reset()
    {
        txtCode_Add.Text = "";
    }
    protected void cbbCategoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ItemDAO itemcategory = new ItemDAO();
        cbbItemList.DataSource = itemcategory.ExecuteSelectFollowCategoryID(cbbCategoryList.SelectedValue.Trim()).Tables[0];
        cbbItemList.DataBind();

        cbbItemList_SelectedIndexChanged(null, null);

        if (cbbItemList.SelectedValue == "")
        {
            btn_AddItems.Enabled = false;
        }
        else
        {
            btn_AddItems.Enabled = true;
        }

    }
    protected void cbbItemList_SelectedIndexChanged(object sender, EventArgs e)
    {
        int itemid = 0;
        int.TryParse(cbbItemList.SelectedValue, out itemid);

        SourceCodeDAO sourcecode = new SourceCodeDAO();
        GridView1.DataSource = sourcecode.ExecuteSelectFollowItemID(itemid).Tables[0];
        GridView1.DataBind();

        Panel_Add.Visible = false;
        Panel_Edit.Visible = false;

    }
}